﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SQLite;

namespace BFTest
{
    public static class NameGenerator
    {
        public static string Generate()
        {
            string firstName = "", lastName = "";

            try
            {
                SQLiteConnection connection = new SQLiteConnection("Data Source=names.s3db");
                connection.Open();
                SQLiteCommand cmd = new SQLiteCommand(connection);
                DataTable result = new DataTable();
                SQLiteDataReader reader;

                // Query for first name
                cmd.CommandText = "SELECT value FROM firstNames ORDER BY random() LIMIT 1";
                reader = cmd.ExecuteReader();
                result.Load(reader);
                firstName = result.Rows[0].ItemArray[0].ToString();
                result.Clear();

                // Query for first name
                cmd.CommandText = "SELECT value FROM lastNames ORDER BY random() LIMIT 1";
                reader = cmd.ExecuteReader();
                result.Load(reader);
                lastName = result.Rows[0].ItemArray[0].ToString();

                reader.Close();
                connection.Close();

                return firstName + " " + lastName;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Name Generator Error:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
    }
}
